
import { StorageService } from './../../../providers/StorageService';
import { StyleService } from './../../../providers/StyleService';
import { BasePage } from './../../basePage';
import { Component, Injector } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { PIC_URL } from "../../../providers/Config";

/**
 * Generated class for the ShopInfoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-shop-info',
  templateUrl: 'shop-info.html',
  providers: [StyleService]
})
export class ShopInfoPage extends BasePage {
  picture: string;
  shop_id: any;
  userinfo: any;
  notRemark: boolean;
  oboaut = false;
  /** 接收数据  用于存储用户的 信息和页面的信息*/
  data = {
    service_id: '',
    user_id:''
  };
  /** 接收id */
  ids: any;

  /*定义一个变量保存是否收藏*/
  isCollection: any;

  service_pic: any; 
  /*用于保存 在确认订单页面显示的服务图片*/

  /** 购买服务数据 */
  goods = {
    id: ''
  };
  picrooturl = ''; //服务器地址
  /** 评论数据 */
  service = {
    service_remark: []
  };
  remark = [];
  remarks = [];
  data3 = {
    user_id: '',
    collected_id: '',
    collect_type: 2
  }
  is_collection=0
  /*用来保存添加至购物车的 信息*/
  data4 = {
    user_id:'',
    goods_id:''
  }
  msg: any;
  constructor(injector: Injector, public styleProvider: StyleService, public stc: StorageService) {
    super(injector);
    this.picrooturl = PIC_URL;
  }
  // ionViewDidEnter(){
  //   this.getBuyServiceAPI();
  // }
  ionViewDidEnter() {
    this.userinfo = this.stc.read('userinfo');
    console.log(this.userinfo);
  console.log( this.isCollection)
    /*获取到当前服务的id*/
    this.ids = this.navParams.get('id');

    /*获取到当前服务的 店铺的id shop_id*/
    
    this.shop_id = this.navParams.get('shop_id');
    /*如果没有传店铺的id 过来 则为 undefined*/ 
    console.log(this.shop_id == undefined)


    this.data.service_id = this.ids;
  
    /*设置没有登录的状态*/
    if(this.userinfo === null) this.data.user_id='';
    else this.data.user_id = this.userinfo.user_id;
    
    console.log(this.data);
    console.log(this.shop_id);
    this.getBuyServiceAPI();
  
  }
  diss() {
    let data = { 'foo': 'bar' };
    this.viewCtrl.dismiss(data);
  }

  /*跳转到店铺详情页面*/
  toShopDetail(){
    console.log("点击到店铺详情页面");
    console.log('当前服务的店铺id 为'+this.shop_id);
    if(this.shop_id != undefined){
      let modal = this.modalCtrl.create('NaiShopDetailPage', { shop_id: this.shop_id });
      modal.present();
    }else{
      console.log("没有得到 店铺信息");
    }
  } 

  /** 跳转到立即购买页面 */
  topromptlyBuy(price, name, market_price,goods_id,service_pic) {
    // this.navCtrl.push('OrderPage');
    // let modal = this.modalCtrl.create('PromptlyBuyPage', { 
      let modal = this.modalCtrl.create('OrderPage', {
      service_price: price,
      nail_name: name,
      market_price:market_price,
      goods_id:goods_id,
      user_id:this.data.user_id,
      service_pic:service_pic });//PromptlyBuyPage  ShopInfoPage
    modal.present();
  }
  /** 获取购买服务详情 */
  async getBuyServiceAPI() {
    // console.log(this.data);
    let res = await this.styleProvider.getBuyServiceAPI(this.data);
    // console.log(res);
    this.goods = res.service.goods;
    this.service_pic = res.service.goods.nail_img[0];
    console.log("确认订单页面的图片"+this.service_pic);
    this.service = res.service;
    this.remark = this.service.service_remark;
    console.log(this.remark);
    if(typeof this.isCollection == 'undefined') this.isCollection = res.service.goods.is_collection;

    if (this.remark.length == 0) {
      this.notRemark = true;
    } else {
      this.notRemark = false;
    }
    console.log(this.goods);
    this.isCollection=this.goods['is_collection']
    console.log(this.remark);
    this.remarks = [];
    this.remarks.push(this.remark[0])
  }

  /**点击收藏 */
  obout(stu) {
    // this.oboaut = stu;
    if(this.userinfo == null){
      this.showAlert("您当前未登录，请登录后操作！");
      return;
    }
    
    this.collectionAPI();

    this.getBuyServiceAPI();
  }
  getToCart() {
    console.log("点击加入购物车");
    this.addToCartAPI();
  }

  /** 更多评论 */
  comment(price, name, market_price,goods_id,service_pic) {
    this.is_collection=this.goods['is_collection']
    this.navCtrl.push('ShopCommentPage', { 
      is_d:this.isCollection,
      comment: this.data ,
      service_price: price,
      nail_name: name,
      market_price:market_price,
      goods_id:goods_id,
    

      user_id:this.data.user_id,
      service_pic:service_pic });
    console.log(this.data)
    
  }


  /** 收藏 */
  async collectionAPI() {
    
    console.log('当前登录的用户的id为'+this.data.user_id);
    this.data3.user_id = this.data.user_id;
    this.data3.collected_id = this.ids;
    this.is_collection=this.ids
    console.log(this.data3);
    let res = await this.styleProvider.collectionAPI(this.data3);
    let res2 = await this.styleProvider.getBuyServiceAPI(this.data);
    console.log(this.data)
    console.log(res2.service.goods.is_collection)
    this.isCollection=res2.service.goods.is_collection
    console.log(res);
    if (res.code == 1) {
      this.msg = res.msg;
      this.presentToast();
    } else {
      this.msg = res.msg;
      this.presentToast();
    }
  }
  
  /*加入购物车*/
  async addToCartAPI(){
    if(this.userinfo==null){
      this.showAlert("您当前未登录，请登录后操作！")
    }else{
      console.log(this.data);
      this.data.user_id = this.userinfo.user_id;
      this.data4.user_id = this.data.user_id;
      this.data4.goods_id = this.data.service_id;
      let res = await this.styleProvider.addToCartAPI(this.data4);
      console.log(res);
      if(res.code == 1){
        this.msg = res.msg;
        this.presentToast();
      }else{
        this.msg = res.msg;
        this.presentToast();
      }
    }
    
  } 


  /** 提示 */
  presentToast() {
    let toast = this.toastCtrl.create({
      message: this.msg,
      duration: 1500,
      position: 'middle',
      cssClass:'shoucang'
    });
    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });

    toast.present();
  }
}
